#ifndef HTTP_RESPONCE
#define HTTP_RESPONCE 1
//static const char CRLF[3]="\r\n";
//HTTP响应报文
//响应行    协议号/版本号 + 状态码 + 状态描述
//响应头    属性名：属性值
//响应体    html中的内容？
//
//exmaple
/*
< HTTP/1.1 200 OK
< Accept-Ranges: bytes
< Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transform
< Connection: keep-alive
< Content-Length: 2381
< Content-Type: text/html
< Date: Mon, 22 Apr 2024 13:01:13 GMT
< Etag: "588604dc-94d"
< Last-Modified: Mon, 23 Jan 2017 13:27:56 GMT
< Pragma: no-cache
< Server: bfe/1.0.8.18
< Set-Cookie: BDORZ=27315; max-age=86400; domain=.baidu.com; path=/
*/
//
//暂时只处理 HTTP/1.1
//将生成好的 响应报文 通过 Buffer_结构 写入 fd
#include <unordered_map>
#include <unordered_set>
#include <sys/stat.h>
#include <sys/mman.h>
#include <fcntl.h>

#include "../buffer/buff.h"
#include "../Logg/log.h"
class Httpresponce
{
enum httpStatusCode 
{
    k100Continue = 100,                 //请求成功
    k200OK = 200,                       //请求处理完成
    k400BadRequest = 400,               //客户端网络出错
    k403Forbidden = 403,                //客户端请求出错 禁止访问
    k404NotFound = 404,                 //客户端请求出错 访问不存在的文件
    k500InternalServerErrno = 500       //服務端出錯
};
public:
    Httpresponce();
    ~Httpresponce();

    void Init(const std::string& srcdir,std::string& path,bool keepalive,int code);
    char *GetFile_();
    size_t GetfileLen_();
    void UnmapFile_();
    void MakeResponce(Buffer_::Buffer& buff);

private:
    void AddStatLine_(Buffer_::Buffer& buff);
    void AddHead_(Buffer_::Buffer& buff);
    void AddContent_(Buffer_::Buffer& buff);
    void SetHtml_();
    void ErronContent(Buffer_::Buffer& buff,std::string message);



    std::string GetFileType_();
    
    int StatusCode_;
    bool isKeepAlive_;

    std::string path_;
    std::string SrcDir_;

    //对文件使用 mmap 映射

    char* mmFile_;
    //通过sata结构体可以快速知道指定 文件/目录是否存在
    struct stat mmFileStat_;    

    static const std::unordered_map<std::string,std::string> SUFFIX_TYPE;   //可以访问的数据类型
    static const std::unordered_map<int,std::string> CODE_STATUS;           //一种状态对应的状态码
    static const std::unordered_map<int,std::string> CODE_PATH;             //错误编码路径集

};
#endif